package en.ameen.preciousengine;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
 
/*!
This is the base class for most of your frame-based animations, and you will 
have to derive from it in order to make a new animation, you get to configure 
the speed of the animation and some other options of your animation.
*/
public class SpriteAnimation extends Entity {
 
	//! The bitmap that contain all the frames of the animation
    private Bitmap mAnimation;
  
    //! The rectangle that surrounds each frame
    private Rect mSRectangle;
    
    //! Frame's duration
    private int mFPS;
    
  	//! Number of frames in the animation
    private int mNoOfFrames;
    
    //! Current frame
    private int mCurrentFrame;
    
    //! Duration of each frame
    private long mFrameTimer;
    
    //! Frame Height
    private int mSpriteHeight;
    
    //! Frame Width
    private int mSpriteWidth;
    
    //! How many loops to do
    private int roundsToGo;
 	
    //! How many loops done
    private int roundsFinished;
    
    //! Flag that indicates if the animation is done
    public boolean finished;
    
    //! SpriteAnimation Constructor
    /*!
      This constructor will initialize all variables.
      \param x Position of text in the x-axis
      \param y Position of text in the y-axis
      \param loopRounds how many times to play the animation
    */
	public SpriteAnimation(int x, int y, int loopRounds) {
		super(x, y);


        mSRectangle = new Rect(0,0,0,0);
        mFrameTimer =0;
        mCurrentFrame =0;
        roundsFinished = 0;
        
        finished = false;
        this.roundsToGo  = loopRounds;
      
    
		
	}
 
    //! Function to load the animation before using it
    /*!
      This function takes the most important parameters so it can play
      the animation
      \param theBitmap the image the contains all frames horizontally aligned
      without padding between frames.
      \param Height the height of each frame
      \param Width the width of each frame
      \param theFPS the desired fps to play the animation with, if you double 
      this number you animation gets to be played faster by double.
      \param theFrameCount how many frames are in this animation
    */
    public void initialize(Bitmap theBitmap, int Height, int Width, int theFPS, int theFrameCount) {
        
    	mAnimation = theBitmap;
        mSpriteHeight = Height;
        mSpriteWidth = Width;
        mSRectangle.top = 0;
        mSRectangle.bottom = mSpriteHeight;
        mSRectangle.left = 0;
        mSRectangle.right = mSpriteWidth;
        mFPS = 1000 / theFPS;
        mNoOfFrames = theFrameCount;
   
    }
  

    //! Update function
    /*!
      updates the animation according to the parameters you initialized
      the animation to.
      \param elapsed elapsed ticks in milliseconds from the previous frame
      \param packet the input packet in case you wanted to handle events
      \param world the world so you can interact with the object there
      
    */
	@Override
	public void update(double elapsed, EventPacket packet, World world) {


		if((System.currentTimeMillis() > mFrameTimer + mFPS) && !finished) {
	            mFrameTimer = System.currentTimeMillis();
	            mCurrentFrame +=1;
	 
	            if(mCurrentFrame >= mNoOfFrames) {
	                
	                roundsFinished++;
	                
		            if (roundsFinished >= roundsToGo){
		            	
		            	finished = true;
		            	mCurrentFrame = mNoOfFrames - 1;
		            			            	
		            }
		            else{
		            	
		            	mCurrentFrame = 0;
		            	
		            }
		            
	            }
	            

	            
	        }
	 
	        mSRectangle.left = mCurrentFrame * mSpriteWidth;
	        mSRectangle.right = mSRectangle.left + mSpriteWidth;
		
	}
	
	
    //! Render function
    /*!
      This function will render the animation object on screen, you can use the
      normal debug options to disable the rendering if you want to.
      \param canvas Canvas to draw on

    */
    @Override
    public void render(Canvas canvas) {
        
   
	    	Rect dest = new Rect(getXPos(), getYPos(), getXPos() + mSpriteWidth,
	                        getYPos() + mSpriteHeight);
	 
	        canvas.drawBitmap(mAnimation, mSRectangle, dest, null);

    	
 }
	 
    //! Function to get the Y post
    /*!

      \return The position of the y-axis

    */
    public int getYPos()
    {
    	return (int)this.y;
    }
    
    //! Function to get the X post
    /*!

      \return The position of the x-axis

    */
    public int getXPos()
    {
    	return (int)this.x;
    }
    
    //! Function to set the position of the animation
    /*!

      \param x the position of the x-axis
      \param y the position of the y-axis

    */
    public void setPosition(int x, int y){
    	
    	this.x = x;
    	this.y = y;
    	
    	
    }
    
    //! Function to set the position of the animation
    /*!

      \param entity Entity to read the position from

    */
    public void setPosition(Entity entity){
    	
    	this.x = entity.x;
    	this.y = entity.y;
    	
    	
    }
    

    //! Function to set the speed of the animation
    /*!

      \param frameDurationInMilliSec Desired frame duration in milliseconds

    */
    public void setSpeed(int frameDurationInMilliSec){
    	
    	mFPS = frameDurationInMilliSec;
    	
    }

}